var app = getApp();
Page({
  data: {
    teamitem: ['团队成员', '签约患者','团队服务包'],
    teamindex: 0,
    myadd:[{},{},{}],
    windowWidth: 0,
    windowHeight: 0,
    t_status: false,
    isopera: false,
    list:[],
    page:0,
    pagesize:10,
    people:[],
  },
  // 团队成员
  teamPerson: function () {
    var id = this.data.id;
    wx.navigateTo({
      url: '/hyb_yl/userCommunicate/pages/teamPerson/teamPerson?id=' + id,
    })
  },
  // 团队服务包
  fuwu: function () {
    var id = this.data.id;
    wx.navigateTo({
      url: '/hyb_yl/backstageTeam/pages/openservice/openservice?id=' + id,
    })
  },
  nodes: function () {
    var id = this.data.id;
    wx.navigateTo({
      url: '/hyb_yl/backstageTeam/pages/notice/notice?id=' + id,
    })
  },
  ishaibao: function () {
    this.setData({
      isopera: true
    })
    this.sharecon();
  },
  closeshare: function () {
    this.setData({
      isopera: false
    })
  },
  sharecon: function () {
    var that = this;
    // var hb = that.data.hbbg;
    // var ewm = that.data.ewm;
    var avatarurl_width = 100;    //绘制的头像宽度
    var avatarurl_heigth = 100;   //绘制的头像高度
    var avatarurl_x = 50;   //绘制的头像在画布上的位置
    var avatarurl_y = 50;   //绘制的头像在画布上的位置
    that.setData({
      t_status: true
    })
    wx.showLoading({
      title: '请等待',
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowWidth: res.screenWidth,
          windowHeight: res.screenHeight
        })
      },
    });
    console.log(that.data.info);
    
    var background = that.data.info.rule.background;
    var thumb = that.data.info.rule.thumb;
    var erweima = that.data.info.erweima;
    var advertisement = that.data.info.advertisement;
    var title = that.data.info.fuze.z_name + "(" + that.data.info.fuze.keshi + that.data.info.fuze.zhicheng + ")";
    var jigou = that.data.info.fuze.jigou;
    wx.getImageInfo({

      src: background,
      success: function (res) {
        console.log(res);
        var haibao = res.path;
        var ctx = wx.createCanvasContext('canvass');
        wx.getImageInfo({
          src: thumb,
          success: function (res) {
            wx.getImageInfo({
              src: erweima,
              success: function (res) {
                var erweima = res.path;
                wx.getImageInfo({
                  src: advertisement,
                  success: function (res) {
                    var touxiang = res.path
                    var urls = res.path;
                    ctx.drawImage(haibao, 0, 0, that.data.windowWidth * 0.9, that.data.windowWidth * 0.8 * 1.79);
                    ctx.drawImage(urls, (that.data.windowWidth * 0.9 - that.data.windowWidth * 0.75) / 2, (that.data.windowWidth * 0.25 * 1 + that.data.windowWidth * 0.1) / 2, that.data.windowWidth * 0.65, that.data.windowWidth * 0.8);
                    // 设置字体
                    ctx.font = "12px bold 黑体";
                    ctx.fillStyle = "#fff";
                    ctx.textAlign = "center";
                    ctx.textBaseline = "middle";
                    ctx.fillText("拥有自己专属的品牌页面", (that.data.windowWidth * 0.9 - that.data.windowWidth * 0.1) / 2, 50);

                    ctx.font = "13px bold 黑体";
                    ctx.fillStyle = "#333";
                    ctx.textAlign = "center";
                    ctx.textBaseline = "middle";
                    ctx.fillText(title, (that.data.windowWidth * 0.9 - that.data.windowWidth * 0.1) / 2, 175);

                    ctx.font = "12px bold 黑体";
                    ctx.fillStyle = "#333";
                    ctx.textAlign = "center";
                    ctx.textBaseline = "middle";
                    ctx.fillText(jigou, (that.data.windowWidth * 0.9 - that.data.windowWidth * 0.1) / 2, 200);

                    ctx.drawImage(advertisement, (that.data.windowWidth * 0.9 / 2) - 50, 90, 60, 60);
                    ctx.drawImage(erweima, (that.data.windowWidth * 0.9 / 2) - 80, 250, 120, 120);

                    wx.hideLoading();
                    that.setData({
                      t_status: true
                    })
                    setTimeout(function () {
                      ctx.draw();
                    }, 100)
                  }
                })
              }
              })
            }
          })
      }
    })
  },

  operation: function () {
    var that = this;
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      canvasId: "canvass",
      success: function (ress) {
        console.log(ress);
        wx.saveImageToPhotosAlbum({
          filePath: ress.tempFilePath,
          success: function (res) {
            wx.showToast({
              title: '保存成功',
            });
            setTimeout(function () {
              that.setData({
                t_status: false,
                isopera: false
              })
            }, 1000)
          },
        })
      }
    })
  },
  onLoad: function (t) {
    var id = t.id;
    this.setData({
      id: id
    })
    this.getDetail()
    this.getpeople()
    this.getService();
  },
  // 获取团队详情
  getDetail: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/team.detail",
      data: {
        id: that.data.id,
        zid:wx.getStorageSync('zid')
      },
      success: function (res) {
        that.setData({
          info: res.data
        })
      }
    })
  },
  // 获取团队签约患者
  getpeople:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/team.team_order",
      data:{
        id:that.data.id,
        page:that.data.page,
        pagesize:that.data.pagesize
      },
      success:function(res){
        var page = that.data.page + 1;
        that.setData({
          page:page,
          people:that.data.people.concat(res.data),
        })
      }
    })
  },
  teamtab: function (e) {
    const index = e.currentTarget.dataset.dex;
    this.setData({
      teamindex: index
    })
    if(index == '0')
    {
      this.setData({
        page:0,
        list:[]
      })
      this.getDetail()
    }else if(index == '1')
    {
      this.setData({
        page:0,
        list:[]
      })
      this.getpeople();
    }else if(index == '2')
    {
      this.setData({
        page:0,
        list:[]
      })
      this.getService();
    }
  },
  // 获取团队服务包
  getService:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/team.servers",
      data:{
        id:that.data.id
      },
      success:function(res){
        that.setData({
          service:res.data
        })
      }
    })
  },
  opening: function (e) {
    console.log(e);
    var id = that.data.id;
    const state = e.currentTarget.dataset.state;
    wx.navigateTo({
      url: '/hyb_yl/backstageTeam/pages/serviceset/serviceset?state=' + state + '&id='+id,
    })
    return false
  },
  twchange: function (e) {
    // console.log(e.currentTarget.dataset.index);
    const ischeck = e.detail.value;
    console.log(ischeck);
    var index = e.currentTarget.dataset.index;
    // this.data.service[index].twcheck = ischeck
    // console.log(this.data.service[index].twcheck);
  },
  twchanges:function (e) {
    
  },
  onReady: function () { },
  onShow: function () {
  },
  tapFlag: function () {
    console.log(111), this.setData({
      teamDescFlag: !this.data.teamDescFlag
    }), this.data.teamDescFlag ? this.setData({
      downFlag: !1
    }) : this.setData({
      downFlag: !0
    });
  },
  // 服务列表
  serve: function (a) {
    var that = this
    var id = that.data.id
    wx.navigateTo({
      url: "/hyb_yl/doctor/pages/familydoctor/category/category?id=" + id
    });
  },
  // 签约患者
  qianyue: function (e) {
    var that = this;
    var id = that.data.id;
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/patientman/patientman?id=' + id,
    })
  },
  list: function (e) {
    var that = this
    var teampic = that.data.teampic
    var t_id = that.data.t_id
    var zid = that.data.zid
    var teamtext = that.data.teamtext
    var teamname = that.data.teamname
    var z_name = that.data.z_name
    var z_thumbs = that.data.z_thumbs
    if (that.data.yaoqing == 1) {
      wx.navigateTo({
        url: "/hyb_yl/backstageTeam/pages/list/list?teampic=" + teampic + '&t_id=' + t_id + '&zid=' + zid + '&teamtext=' + teamtext + '&teamname=' + teamname + '&z_name=' + z_name + '&z_thumbs=' + z_thumbs + '&yaoqing=' + that.data.yaoqing
      });
    } else {
      wx.navigateTo({
        url: "/hyb_yl/backstageTeam/pages/list/list?teampic=" + teampic + '&t_id=' + t_id + '&zid=' + zid + '&teamtext=' + teamtext + '&teamname=' + teamname + '&z_name=' + z_name + '&z_thumbs=' + z_thumbs
      });
    }
  },
  clickname: function (a) {
    var t = this;
    wx.navigateTo({
      url: "/hyb_yl/backstageOther/pages/teamDetails1/teamDetails1?teamName=" + t.data.detailList.teamName
    });
  },
  clicknameText: function (a) {
    wx.navigateTo({
      url: "/hyb_yl/backstageOther/pages/teamDetails2/teamDetails2?briefIntroduce=" + this.data.detailList.teamDesc
    });
  },
  member: function (a) {
    wx.navigateTo({
      url: "/hyb_yl/backstageTeam/pages/teamDetails3/teamDetails3"
    });
  },
  Wimg: function (t) {
    var e = this,
      o = (wx.getStorageSync("log"), wx.getStorageSync("teamId") || "");
    wx.chooseImage({
      count: 1,
      sizeType: ["orignal", "compressed"],
      sourceType: ["album", "camera"],
      success: function (t) {
        console.log(t.tempFilePaths[0]), e.setData({
          img: t.tempFilePaths[0]
        });
        var i = wx.getStorageSync("log") || "";
        wx.uploadFile({
          url: a.globalData.dic + "doctor/team/update/" + i,
          filePath: t.tempFilePaths[0],
          name: "file",
          formData: {
            teamId: o,
            flag: 1
          },
          method: "POST",
          header: {
            "Content-Type": "multipart/form-data"
          },
          success: function (t) {
            console.log(t), wx.request({
              url: a.globalData.dic + "doctor/team/detail/" + e.data.token + "/" + e.data.teamId,
              success: function (a) {
                if (console.log(a), 200 == a.data.code) {
                  wx.hideLoading();
                  var t = a.data.data.createTime.split(" ");
                  a.data.data.createTime = t[0], e.setData({
                    detailList: a.data.data,
                    img: a.data.data.teamHeaderUrl
                  }), console.log(e.data.detailList);
                }
              }
            });
          }
        });
      }
    });
  },
  navCode: function (e) {
    var that = this
    var t_id = that.data.t_id
    var z_thumbs = that.data.z_thumbs
    var z_name = that.data.z_name
    //生成二维码
    a.util.request({
      'url': 'entry/wxapp/Studio.teamerweima',
      data: {
        t_id: t_id,
      },
      success: function (res) {
        console.log(res)
        var tderweima = res.data.tderweima
        var info = JSON.stringify(res.data)
        // wx.navigateTo({
        //   url: '/hyb_yl/backstageLifes/pages/dialysis/dialysis?info= ' + info + '&weweima=' + weweima,
        // })
        wx.navigateTo({
          url: "/hyb_yl/backstageTeam/pages/teamDetails4/teamDetails4?info=" + info + '&tderweima=' + tderweima + '&z_name=' + z_name + '&z_thumbs=' + z_thumbs
        });
      },
    })



  },
  shanchu: function (t) {
    var e = this;
    wx.request({
      url: a.globalData.dic + "doctor/team/getTeamUnfinishedOrder",
      data: {
        teamId: e.data.teamId
      },
      success: function (t) {
        console.log(), t.data.data ? wx.showModal({
          title: "请先结束未完成咨询！",
          showCancel: !1,
          confirmColor: "#3b4ca9",
          confirmText: "取消",
          success: function (a) {
            a.confirm || a.cancel;
          }
        }) : wx.showModal({
          title: "提示",
          content: "您确认解散团队吗？",
          success: function (t) {
            t.confirm ? wx.request({
              url: a.globalData.dic + "doctor/team/break",
              data: {
                token: e.data.token,
                teamId: e.data.teamId
              },
              method: "POST",
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              success: function (a) {
                if (console.log(a.data), 200 == a.data.code) {
                  if (1 == e.data.groupFlag) return void wx.navigateBack({
                    delta: 3
                  });
                  wx.navigateBack({
                    delta: 2
                  });
                }
              }
            }) : t.cancel && console.log("用户点击取消");
          }
        });
      }
    });
  },
  shanchu1: function (t) {
    var e = this;
    wx.showModal({
      title: "提示",
      content: "您确认退出团队吗？",
      success: function (t) {
        t.confirm ? wx.request({
          url: a.globalData.dic + "doctor/team/quit/" + e.data.token + "/" + e.data.teamId,
          success: function (a) {
            if (console.log(a.data.code), 200 == a.data.code) {
              if (1 == e.data.groupFlag) return void wx.navigateBack({
                delta: 3
              });
              wx.navigateBack({
                delta: 2
              });
            }
          }
        }) : t.cancel && console.log("用户点击取消");
      }
    });
  },
  invitation: function (a) {
    wx.navigateTo({
      url: "/hyb_yl/backstageOther/pages/teamDetails5/teamDetails5"
    });
  },

  //查询是否是团队队长
  getMyiftongyi: function () {
    var that = this
    var t_id = that.data.t_id
    a.util.request({
      'url': 'entry/wxapp/Yaoqing',
      data: {
        t_id: t_id,
        openid: wx.getStorageSync('openid'),
        op: 'ifduiz'
      },
      success: function (res) {
        console.log(res)
        if (res.data == 1) {
          that.setData({
            type: 0
          })
        } else {
          that.setData({
            type: 1
          })
        }
      }
    })
  },
});